﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Filters;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Areas.Manage.Controllers
{
    /// <summary>
    /// 热门文章链接的控制器
    /// </summary>
    [Auth(AuthType = WqUserAuth.Manage)]
    public class HotController : BaseManageController
    {
        /// <summary>
        /// 热门文章
        /// </summary>
        /// <returns></returns>
        public IActionResult Article(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<article_hot> list;

            var query = DbContext.article_hots.Where(x => x.is_show);
            var page = new PageModel() { CurrentPageIndex = pageIndex };
            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.title.Contains(search) || x.url.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["page"] = page;
            ViewData["list"] = list;
            ViewData["is_show"] = true;
            ViewData["show-count"] = DbContext.article_hots.Where(x => x.is_show).Count();
            ViewData["hide-count"] = DbContext.article_hots.Where(x => x.is_show == false).Count();

            return View();
        }

        /// <summary>
        /// 热门文章
        /// </summary>
        /// <returns></returns>
        public IActionResult ArticleHide(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<article_hot> list;

            var query = DbContext.article_hots.Where(x => x.is_show == false);
            var page = new PageModel() { CurrentPageIndex = pageIndex };
            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.title.Contains(search) || x.url.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["page"] = page;
            ViewData["list"] = list;
            ViewData["is_show"] = false;
            ViewData["show-count"] = DbContext.article_hots.Where(x => x.is_show).Count();
            ViewData["hide-count"] = DbContext.article_hots.Where(x => x.is_show == false).Count();

            return View("Article");
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Save(article_hot model)
        {
            if (!string.IsNullOrWhiteSpace(model.title) && !string.IsNullOrWhiteSpace(model.url))
            {
                model.create_time = DateTimeOffset.UtcNow;
                model.last_update = DateTimeOffset.UtcNow;
                model.change_user_name = LoginUser.UserName;
                DbContext.article_hots.Add(model);
                DbContext.SaveChanges();
            }
            return JsonOkResult("保存成功");
        }

        /// <summary>
        /// 详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Detail(Guid id)
        {
            var model = DbContext.article_hots.Where(x => x.id == id).SingleOrDefault() ?? new article_hot();

            return View(model);
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Update(article_hot model)
        {
            if (!string.IsNullOrWhiteSpace(model.title) && !string.IsNullOrWhiteSpace(model.url))
            {
                var hot = DbContext.article_hots.Where(x => x.id == model.id).SingleOrDefault();
                if (hot != null)
                {
                    hot.change_user_name = LoginUser.UserName;
                    hot.title = model.title;
                    hot.url = model.url;
                    hot.sort_num = model.sort_num;
                    hot.last_update = DateTimeOffset.UtcNow;
                    DbContext.SaveChanges();
                }
            }

            return JsonOkResult("保存成功");
        }

    }
}